package com.mzj.saas.mservice.eligible.repository;

import com.mzj.saas.mservice.eligible.entity.DepartmentHierarchy;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface DepartmentHierarchyRepository extends PagingAndSortingRepository<DepartmentHierarchy, Long>, JpaSpecificationExecutor<DepartmentHierarchy> {
    DepartmentHierarchy findByAreaId(Long areaId);

    DepartmentHierarchy findByOrgId(Long orgId);

    List<DepartmentHierarchy> findByOrgIdIn(List<Long> orgIds);

    List<DepartmentHierarchy> findByIdInAndTagId(List<Long> id, Long tagId);

    DepartmentHierarchy findByOrgIdAndTagId(Long orgId, Long tagId);

    //根据机构id查出机构层级关系
    List<DepartmentHierarchy> findByIdIn(List<Long> ids);

    DepartmentHierarchy findByOrgIdAndStatus(Long orgId, Integer status);
    DepartmentHierarchy findByOrgIdAndTagIdIn(Long orgId,List<Long> tagId);

    DepartmentHierarchy findByAreaIdAndStatus(Long areaId,Integer status);
    DepartmentHierarchy findByAreaIdAndTagId(Long areaId,Long tagId);

    DepartmentHierarchy findByNameAndStatus(String name , Integer status);

    @Query(nativeQuery = true, value = "select * from `saas_department_hierarchy` where saas_department_hierarchy.org_id in (select id from `saas_department` where id in (select dep_id from `saas_user_department_ref` where saas_user_department_ref.user_id in  (select id from  `saas_user` a WHERE a.mobile= :phone)))")
    List<DepartmentHierarchy> DepartmentId(@Param("phone") String phone);

    @Query(nativeQuery = true, value = "select name from `saas_department_hierarchy` s where s.id = :id")
    String DepartmentName(@Param("id") Long id);
}
